package com.xindacloud.sms.adapter.handler;


import com.xindacloud.sms.adapter.exception.BusinessException;
import com.xindacloud.sms.adapter.exception.ServiceException;
import com.xindacloud.sms.adapter.exception.UnknowException;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import com.xindacloud.sms.adapter.utils.ValidateMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.dao.DataAccessException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.client.ResourceAccessException;

import java.net.ConnectException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.xindacloud.sms.adapter.constant.SmsError.*;
import static com.xindacloud.sms.adapter.utils.UnionResultHelp.StatusEnum.InnerError;


@Slf4j
public abstract class AbstractSmsExceptionHandler {

    Object transferException(Exception e) {


        if (e instanceof BusinessException) {
            return buildUnionResult(UnionResultHelp.StatusEnum.Fail.code(), e.getMessage());
        }

        if (e instanceof ServiceException) {
            ServiceException se = (ServiceException) e;
            return buildUnionResult(se.getErrorCode(), se.getMessage());
        }

        if (e instanceof MethodArgumentNotValidException) {
            BindingResult bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
            UnionResultHelp result = new UnionResultHelp();
            result.setStatus(UnionResultHelp.StatusEnum.ParamsFail.code());
            List<ValidateMessage> list = new ArrayList<>();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {

                list.add(ValidateMessage.builder().
                        defaultMessage(fieldError.getDefaultMessage()).
                        field(fieldError.getField()).
                        objectName(fieldError.getObjectName()).build());
            }
            result.setMessage("参数验证不通过");
            result.setData(list);
            result.setIsValid(true);
            return result;
        }

        if (e instanceof UnknowException) {
            UnknowException ue = (UnknowException) e;
            return buildUnionResult(InnerError.code(), ue.getMessage());
        }

        if (e instanceof NullPointerException) {
            return buildUnionResult(InnerError.code(), BA_NULL_POINT.getTips());
        }
        if (e instanceof PersistenceException) {
            return buildUnionResult(InnerError.code(), PERSISTENCE_EXCEPTION.getTips());
        }

        if (e instanceof SQLException) {
            return buildUnionResult(InnerError.code(), DB_EXCEPTION.getTips());
        }

        if (e instanceof DataAccessException) {
            return buildUnionResult(InnerError.code(), DB_EXCEPTION.getTips());
        }

        if (e instanceof ResourceAccessException) {
            return buildUnionResult(InnerError.code(), SMS_ERROR.getTips());

        }

        if (e instanceof ConnectException) {
            return buildUnionResult(InnerError.code(), SMS_ERROR.getTips());
        }

        log.warn("未处理异常脱敏异常信息.", e);
        return buildUnionResult(InnerError.code(), UNKNOW_EXCEPTION.getTips());
    }

    private UnionResultHelp<Object> buildUnionResult(int status, String message) {
        return UnionResultHelp
                .builder()
                .status(status)
                .message(message)
                .timestamp(UnionResultHelp.getStrTimestamp())
                .isValid(false)
                .build();
    }
}

